<?php

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];
    public function blogs(){
        return $this->hasMany(Blog::class);
    }
    //获取所有粉丝
    public function follows(){
        return $this->belongsToMany(User::class,'follows','user_id','follower');
    }
    //获取所有关注
    public function following(){
        return $this->belongsToMany(User::class,'follows','follower','user_id');
    }
    //判断此uid是否是我的粉丝
    public function isFollow($uid){
        return $this->follows()->wherePivot('follower',$uid)->first();
    }
    //关注取消关注
    public function followToggle($ids){
        $ids=is_array($ids)?$ids:[$ids];
        return $this->follows()->withTimestamps()->toggle($ids);
    }
}
